<?PHP 

include_once($CFG->dirroot . '/course/lib.php');

class block_filtered_course_list extends block_list {
  function init() {
    $this->title = get_string('blockname', 'block_filtered_course_list');
    $this->version = 20080109;
  }
    
  function has_config() {
    return true;
  }
  
  function preferrred_width() {
    return 200;
  }

  function get_content() {
    global $THEME, $CFG, $USER;
  
    /*if($this->content !== NULL) {
      return $this->content;
    }*/
  
    $this->content = new stdClass;
    $this->content->items = array();
    $this->content->icons = array();
    $this->content->footer = '';
  
    $icon  = "<img src=\"$CFG->pixpath/i/course.gif\"".
              " class=\"icon\" alt=\"".get_string("course")."\" />";
  
    $adminseesall = true;
    if (isset($CFG->block_filtered_course_list_adminview)) {
      if ( $CFG->block_filtered_course_list_adminview == 'own'){
        $adminseesall = false;
      }
    }
    
    if (isset($CFG->block_filtered_course_list_title)) {
      $this->title = $CFG->block_filtered_course_list_title;
    }
    
    $all_courses = get_my_courses($USER->id);
  
    if ($all_courses) {
      $filtered_courses = $this->_filter_courses($all_courses);
  
      foreach($filtered_courses as $section => $course_list) {
        if (count($course_list) == 0) {
          continue;
        }
        
        $this->content->items[]= "<center>$section</center>";
        $this->content->icons[]= '';
    
        foreach ($course_list as $course) {
          $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
          $this->content->items[]= "<a $linkcss title=\"" . format_string($course->shortname) . "\" ".
            "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">" . format_string($course->fullname) . "</a>";
          $this->content->icons[]= $icon;
        }
  
        /// If we can update any course of the view all isn't hidden, show the view all courses link
      
        if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM)) || 
            empty($CFG->block_filtered_course_list_hideallcourseslink)) {
          $this->content->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".
            get_string("fulllistofcourses")."</a> ...";
        }
      }
    }
    
    return $this->content;
  }
  
  function _filter_courses($courses) {    
    $results = array(
      'Current Courses' => array(),
      'Old Courses' => array()
    );
    
    foreach($courses as $course) {
      if ($course->id == SITEID) {
        continue;
      }
        
      if (preg_match('/^200730/', $course->idnumber)) {
        $results['Current Courses'][]= $course;
      } else {
        $results['Old Courses'][]= $course;
      }
    }
    
    return $results;
  }
}

?>
